package be.jabapage.snooker.service.competition.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.apache.commons.lang.Validate;

import be.jabapage.snooker.exception.EntityNotFoundException;
import be.jabapage.snooker.jdo.competition.CompetitionGame;
import be.jabapage.snooker.service.competition.api.ICompetitionGameService;


public class AppEngineCompetitionGameService implements ICompetitionGameService, Serializable {

	private static final long serialVersionUID = -4416423034477195215L;
	private final PersistenceManagerFactory pmf;

	public AppEngineCompetitionGameService(final PersistenceManagerFactory pmf) {
		Validate.notNull(pmf,"The persistence Manager factory cannot be null");
		this.pmf = pmf;
	}

	/**
	 * {@inheritDoc}
	 */
	public void store(final CompetitionGame competitionGame) {
		Validate.notNull(competitionGame);
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			pm.makePersistent(competitionGame);
		} finally {
			pm.close();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public CompetitionGame retrieve(final Long key) throws EntityNotFoundException {
		Validate.notNull(key, "A key should be provided.");
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			CompetitionGame objectById = pm.getObjectById(CompetitionGame.class, key);
			if (objectById == null) {
				throw new EntityNotFoundException();
			}
			return objectById;
		} finally {
			pm.close();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Set<CompetitionGame> retrieveForTeam(final Long teamId) {
		Validate.notNull(teamId);
		PersistenceManager pm = pmf.getPersistenceManager();
		Set<CompetitionGame> resultSet = new HashSet<CompetitionGame>();
		Query query = pm.newQuery(CompetitionGame.class);
		try {
			query.setFilter("teamId == teamParam");
			query.declareParameters("Long teamParam");
			@SuppressWarnings("unchecked")
			List<CompetitionGame> result = (List<CompetitionGame>) query.execute(teamId);
			if (result != null) {
				resultSet.addAll(result);
			}
		} finally {
			query.closeAll();
			pm.close();
		}
		return resultSet;
	}

	public void delete(final Long key) throws EntityNotFoundException {
		Validate.notNull(key, "We cannot delete an instance withour a key.");
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			CompetitionGame dataInstance = pm.getObjectById(CompetitionGame.class, key);
			if (dataInstance == null) {
				throw new EntityNotFoundException();
			}

			pm.deletePersistent(dataInstance);
		} finally {
			pm.close();
		}
	}

	public CompetitionGame retrieveForTeamAndWeek(final Long teamId, final Long weekId)
	throws EntityNotFoundException {
		Validate.notNull(teamId);
		Validate.notNull(weekId);
		PersistenceManager pm = pmf.getPersistenceManager();
		Query query = pm.newQuery(CompetitionGame.class);
		try {
			query.setFilter("teamId == teamParam and week == weekParam");
			query.declareParameters("Long teamParam, Long weekParam");
			@SuppressWarnings("unchecked")
			List<CompetitionGame> execute = (List<CompetitionGame>) query.execute(teamId, weekId);
			if (execute == null || execute.isEmpty()) {
				throw new EntityNotFoundException();
			}

			return execute.get(0);

		} finally {
			query.closeAll();
			pm.close();
		}
	}
}
